package com.chixing.ih.module.admin.mapper.Provider;

import com.chixing.ih.util.EmptyUtil;
import org.apache.ibatis.jdbc.SQL;

public class AdminOrderMapperProvider {

    //0-正在进行;1，3-待审核;2-已完成;4-已退款
    public String getSettlementingOrder(String keyword) {
        return new SQL() {{
            SELECT("o.order_id, o.order_num, o.order_status, u1.nick_name send_user, u2.nick_name receive_user,t.id task_id,t.task_title, o.salary,t.work_cycle AS workCycle,t.publishing_phone AS isSuerTelephone");
            FROM(" tbl_order o");
            JOIN("tbl_task t on task_id = t.id");
            JOIN("tbl_user u1 on send_task_user_id = u1.user_id");
            JOIN("tbl_user u2 on receive_task_user_id = u2.user_id");
            if (!EmptyUtil.isEmpty(keyword)) {
                WHERE("u1.nick_name LIKE CONCAT('%',#{keyword},'%')");
            }
            AND();
            WHERE("o.order_status=1");
            ORDER_BY("o.update_time DESC");
        }}.toString();
    }

    public String getRefusingOrder(String keyword) {
        return new SQL() {{
            SELECT("o.order_id, o.order_num, o.order_status, u1.nick_name send_user, u2.nick_name receive_user,t.id task_id,t.task_title, o.salary,t.work_cycle AS workCycle,t.publishing_phone AS isSuerTelephone");
            FROM(" tbl_order o");
            JOIN("tbl_task t on task_id = t.id");
            JOIN("tbl_user u1 on send_task_user_id = u1.user_id");
            JOIN("tbl_user u2 on receive_task_user_id = u2.user_id");
            if (!EmptyUtil.isEmpty(keyword)) {
                WHERE("u1.nick_name LIKE CONCAT('%',#{keyword},'%')");
            }
            AND();
            WHERE("o.order_status=3");
            ORDER_BY("o.update_time DESC");
        }}.toString();
    }

    public String getCompletedOrder() {
        return new SQL() {{
            SELECT("o.order_id, o.order_num, o.order_status, u1.nick_name send_user, u2.nick_name receive_user,t.id task_id, t.task_title, o.salary,t.work_cycle AS workCycle,t.publishing_phone AS isSuerTelephone");
            FROM(" tbl_order o");
            JOIN("tbl_task t on task_id = t.id");
            JOIN("tbl_user u1 on send_task_user_id = u1.user_id");
            JOIN("tbl_user u2 on receive_task_user_id = u2.user_id");
            WHERE("order_status = 2");
            ORDER_BY("o.update_time DESC");
        }}.toString();
    }

    public String getRefundedOrder() {
        return new SQL() {{
            SELECT("o.order_id, o.order_num, o.order_status, u1.nick_name send_user, u2.nick_name receive_user,t.id task_id, t.task_title, o.salary,t.work_cycle AS workCycle,t.publishing_phone AS isSuerTelephone");
            FROM(" tbl_order o");
            JOIN("tbl_task t on task_id = t.id");
            JOIN("tbl_user u1 on send_task_user_id = u1.user_id");
            JOIN("tbl_user u2 on receive_task_user_id = u2.user_id");
            WHERE("order_status = 4");
            ORDER_BY("o.update_time DESC");
        }}.toString();
    }

    public String agreeToSettlement(Integer id) {
        return new SQL() {{
            UPDATE("tbl_order");
            SET("order_status = 2 ");
            WHERE("order_id = #{id}");
        }}.toString();
    }

    public String agreeToRefund(Integer id) {
        return new SQL() {{
            UPDATE("tbl_order");
            SET("order_status = 4");
            WHERE("order_id = #{id}");
        }}.toString();
    }

    public String setIsDeleted(Integer id) {
        return new SQL() {{
            UPDATE("tbl_task");
            SET("is_deleted = 1");
            WHERE("id = #{id}");
        }}.toString();
    }

    public String getAllOrder(String keyword) {
        return new SQL() {{
            SELECT("o.order_num,o.payment_status AS paymentStatus,o.order_status, o.salary,o.order_begin_time,o.order_end_time, u1.nick_name send_user, u2.nick_name receive_user,t.task_title,t.talent_categories,t.work_way,t.requirement_description,t.technology,t.work_cycle,t.publishing_name,t.publishing_phone");
            FROM(" tbl_order o");
            JOIN("tbl_task t on task_id = t.id");
            JOIN("tbl_user u1 on send_task_user_id = u1.user_id");
            JOIN("tbl_user u2 on receive_task_user_id = u2.user_id");
            if (!EmptyUtil.isEmpty(keyword)) {
                WHERE("t.task_title LIKE CONCAT('%',#{keyword},'%')");
            }
            WHERE("o.order_status in (0,2,4)");
            ORDER_BY("o.update_time DESC");
        }}.toString();
    }
}
